/**
 * SplayTreeMapTests
 *
 * @author Chris Pratt
 *
 * 12/11/2011
 */
package com.anodyzed.onyx.util;

import com.anodyzed.onyx.log.LogBuilder;
import com.anodyzed.onyx.log.sysout.SysOutLogFactory;

import java.util.Map;

import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;

public class SplayTreeMapTests {

  /**
   * Setup the Test Harness
   */
  @BeforeClass
  public static void setup () {
    LogBuilder.setLogFactory(new SysOutLogFactory());
  } //setup

  /**
   * Perform a jUnit Test on the Splay Tree
   */
  @Test 
  public void testTree () {
    SplayTreeMap<Integer,Integer> t = new SplayTreeMap<Integer,Integer>();
    final int NUMS = 40000;
    final int GAP  =   307;

    System.out.println("Checking... (no bad output means success)");
    for(int i = GAP;i != 0;i = (i + GAP) % NUMS) {
      t.put(i,i);
    }
    System.out.println("Inserts complete");
    for(int i = 1;i < NUMS;i += 2) {
      t.remove(i);
    }
    System.out.println("Removes complete");
    if((t.firstKey() != 2) || (t.lastKey() != NUMS - 2)) {
      fail("FindMin or FindMax error!");
    }
    for(int i = 2;i < NUMS;i += 2) {
      if(t.find(i).getValue() != i) {
        fail("Error: find fails for " + i);
      }
    }
    for(int i = 1;i < NUMS;i += 2) {
      if(t.find(i) != null) {
        fail("Error: Found deleted item " + i);
      }
    }
  } //testTree

  /**
   * Perform a Standard Map Test
   */
  @Test
  public void testStandardMap () {
    Map<String,Integer> map = new SplayTreeMap<String,Integer>();
    map.put("Test",1);
    map.put("Test",2);
    map.put("Test",1);
    assertEquals(1,map.get("Test").intValue());
    for(int i = 0; i < 100; i++) {
      map.put(String.valueOf(i),i);
    }
    assertEquals(101,map.size());
    assertEquals(51,map.get("51").intValue());
    int n = 0;
    for(String k : map.keySet()) {
      if(n < 100) {
        assertEquals(n++,Integer.parseInt(k));
      } else {
        assertEquals("Test",k);
      }
    }
  } //testStandardMap

  /**
   * jUnit 3.x Test Suite
   *
   * @return Adapted Test Suite
   */
  public static junit.framework.Test suite () {
    return new junit.framework.JUnit4TestAdapter(SplayTreeMapTests.class);
  } //suite

} //*SplayTreeMapTests
